/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Appointment;
import model.Task;
import model.user.Staff;
import util.Utilization;
import view.user.ACIListModel;
import view.user.StaffView;

/**
 *
 * @author CUNEYT
 */
public class StaffController {

    private class SeeWorkHoursButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getWorkHoursPanel());
            view.getAppointmentList().clearSelection();
            view.getTaskList().clearSelection();
        }
    }

    private class SeeScheduleButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getSchedulePanel());
            view.getAppointmentList().clearSelection();
            view.getTaskList().clearSelection();
        }
    }

    private class AppointmentSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getAppointmentList().getSelectedValue() != null) {
                Appointment app = (Appointment) view.getAppointmentList().getSelectedValue();
                view.getAppDateTextField().setText(Utilization.formatDate(app.getDate()));
                view.getAppStartTimeTextField().setText(app.getStartTime());
                view.getAppEndTimeTextField().setText(app.getEndTime());
                view.getAppExplanationTextArea().setText(app.getExplanataion());
            } else {
                view.getAppDateTextField().setText("");
                view.getAppStartTimeTextField().setText("");
                view.getAppEndTimeTextField().setText("");
                view.getAppExplanationTextArea().setText("");
            }
        }
    }

    private class TaskSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getTaskList().getSelectedValue() != null) {
                Task task = (Task) view.getTaskList().getSelectedValue();
                view.getTaskStartDateTextField().setText(Utilization.formatDate(task.getDate()));
                view.getTaskEndDateTextField().setText(Utilization.formatDate(task.getEndDate()));
                view.getTaskStartTimeTextField().setText(task.getStartTime());
                view.getTaskEndTimeTextField().setText(task.getEndTime());
                view.getTaskExplanationTextField().setText(task.getExplanataion());
                if (task.getRelatedTo() != null) {
                    view.getRelatedCampaignTextField().setText(task.getRelatedTo().toString());
                }
                if (task.getSkill() != null) {
                    view.getSkillsRequiredTextField().setText(task.getSkill().toString());
                }
            } else {
                view.getTaskStartDateTextField().setText("");
                view.getTaskEndDateTextField().setText("");
                view.getTaskStartTimeTextField().setText("");
                view.getTaskEndTimeTextField().setText("");
                view.getRelatedCampaignTextField().setText("");
                view.getSkillsRequiredTextField().setText("");
                view.getTaskExplanationTextField().setText("");
            }
        }
    }
    private Staff staff;
    private StaffView view;

    public StaffController(Staff s, StaffView sv) {
        staff = s;
        view = sv;
        staff.addObserver(view);
        view.getTaskList().setModel(new ACIListModel(staff.getTasks()));
        view.getTaskList().addListSelectionListener(new TaskSelectedListener());
        view.getAppointmentList().setModel(new ACIListModel(staff.getAppointments()));
        view.getAppointmentList().addListSelectionListener(new AppointmentSelectedListener());
        view.getWorkHoursList().setModel(new ACIListModel(staff.seeWorkHours()));
        view.getTotalInOfficeWorkHoursTextField().setText(String.valueOf(staff.getInOfficeHours()));
        view.getTotalOutOfOfficeWorkHoursTextField().setText(String.valueOf(staff.getOutOfOfficeHours()));
        view.getSeeScheduleButton().addActionListener(new SeeScheduleButtonListener());
        view.getSeeWorkHoursButton().addActionListener(new SeeWorkHoursButtonListener());
    }

    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }

    public StaffView getView() {
        return view;
    }

    public void setView(StaffView view) {
        this.view = view;
    }

    public void changeForePanel(javax.swing.JPanel forePanel) {
        view.getStaffLayeredPane().setLayer(view.getSchedulePanel(), 0);
        view.getSchedulePanel().setVisible(false);
        view.getStaffLayeredPane().setLayer(view.getWorkHoursPanel(), 1);
        view.getWorkHoursPanel().setVisible(false);
        view.getStaffLayeredPane().setLayer(forePanel, 2);
        forePanel.setVisible(true);
    }
}
